package com.fujica.invoicing.service.impl;

import com.fujica.invoicing.entity.*;
import com.fujica.invoicing.mapper.*;
import com.fujica.invoicing.request.sell.SellGoods;
import com.fujica.invoicing.request.sell.SellReq;
import com.fujica.invoicing.service.ISellService;
import com.fujica.invoicing.utils.Result;
import com.fujica.invoicing.utils.ResultBody;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SellServiceImpl
 * @Description TODO
 * @Author admin
 * @Date 2020/8/11 14:12
 * @Version 1.0
 **/
@Service
@Slf4j
public class SellServiceImpl implements ISellService {

    @Autowired
    private ISellMapper iSellMapper;

    @Autowired
    private ISellDetailMapper iSellDetailMapper;

    @Autowired
    private IInventoryMapper iInventoryMapper;

    @Autowired
    private IGoodsCostMapper iGoodsCostMapper;

    @Autowired
    private IGoodsMapper iGoodsMapper;

    @Autowired
    private IStoreMapper iStoreMapper;

    @Autowired
    private ISellPayMapper iSellPayMapper;

    /**
     * 售货
     * @param req
     * @return
     *
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result sell(SellReq req) {

        // 当前时候
        Date now = new Date();

        // 门店id
        Integer sId = req.getStoreId();
        // 员工id
        Integer eId = 1;
        // 实收金额
        BigDecimal actuallyMoney = req.getActuallyMoney();
        // 找零
        BigDecimal change = req.getChange();
        // 支付方式集合
        List<Integer> payMethods = req.getPayMethods();
        // 商品集合
        List<SellGoods> sellGoods = req.getSellGoods();

//         获取所有商品的销售总金额
        BigDecimal sAmount = sellGoods.stream().reduce(BigDecimal.ZERO, (x, y) -> {
            return x.add(y.getGoodsPrice().multiply(BigDecimal.valueOf(y.getGoodsNum())));
        }, BigDecimal::add);

        // 操作销售单表
        // 新增销售单数据 获取销售id
        TSell sell = new TSell(sId, eId, sAmount, actuallyMoney, change, now);
        iSellMapper.insertSell(sell);
        // 获取销售id
        Integer sellId = sell.getId();

        // 操作销售明细表
        sellGoods.forEach(item -> {
            Integer gId = item.getGoodsId();
            // 根据商品id查找当前商品名称
            String gName = iGoodsMapper.selectByPrimaryKey(gId).getGName();
            BigDecimal gPrice = item.getGoodsPrice();
            Integer gNum = item.getGoodsNum();
            TSellDetail sellDetail = new TSellDetail(sellId, gId, gName, gPrice, gNum);
            // 插入销售明细数据
            iSellDetailMapper.insert(sellDetail);

            // 操作库存表
            // 根据门店查找仓库id
            Integer dId = iStoreMapper.selectByPrimaryKey(sId).getDId();
            // 查找对应仓库id、商品id的库存记录
            Map invoicingMap = Maps.newHashMap();
            invoicingMap.put("dId", dId);
            invoicingMap.put("gId", gId);
            List<TInventory> inventoryList = iInventoryMapper.list(invoicingMap);
            TInventory inventory = inventoryList.stream().min((x, y) -> x.getGPrice().compareTo(y.getGPrice())).get();
            // 商品的最低进货价
            BigDecimal purPrice = inventory.getGPrice();
            // 操作商品的库存量 对应减少
            inventory.setInventory(inventory.getInventory() - gNum);
            inventory.setIUpdate(now);
            // 更新数据库
            iInventoryMapper.updateByInventory(inventory);

            // 商品成本表
            TGoodsCost goodsCost = new TGoodsCost(sellId, gId, purPrice, gNum);
            // 添加商品成本
            iGoodsCostMapper.insert(goodsCost);

            // 销售多支付方式表
            payMethods.forEach(payMethodId -> {
                // 插入数据库
                TSellPay sellPay = new TSellPay(sellId, payMethodId);
                iSellPayMapper.insert(sellPay);
            });


        });

        return ResultBody.ok();
    }
}
